//
//  UIButton+LayoutUtilities.m
//  QXN_Bird
//
//  Created by ChenWei on 16/6/20.
//  Copyright © 2016年 chou. All rights reserved.
//

#import "UIButton+LayoutUtilities.h"
#import <objc/runtime.h>




@implementation UIButton (LayoutUtilities)
static char isFixedWidthKey;

- (BOOL)isFixedWidth {
    
    return [objc_getAssociatedObject(self, &isFixedWidthKey) boolValue];
}

- (void)setIsFixedWidth:(BOOL)isFixedWidth {
    objc_setAssociatedObject(self, &isFixedWidthKey, @(isFixedWidth), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)fixAutoLayoutWidth:(CGFloat)fixWidth {
    if (!self.isFixedWidth) {
        self.isFixedWidth = true;
//        [self.superview layoutIfNeeded];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo([self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].width+fixWidth);
        }];
    }
}

- (instancetype)initBorderLineStyleWithTitle:(NSString *)title
                                 borderColor:(UIColor *)borderColor
                                  titleColor:(UIColor *)titleColor{
    self = [super init];
    if (self) {
        self.layer.borderColor = borderColor.CGColor;
        self.layer.borderWidth = 0.5;
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:titleColor forState:UIControlStateNormal];
    }
    return self;
}

- (instancetype)initFillColorStyleWithTitle:(NSString *)title
                                  fillColor:(UIColor *)fillColor
                                 titleColor:(UIColor *)titleColor {
    self = [super init];
    if (self) {
        self.backgroundColor = fillColor;
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:titleColor forState:UIControlStateNormal];
    }
    return self;
}

- (void)setLeftTextAndRightImage {
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.imageView.bounds.size.width, 0, self.imageView.bounds.size.width)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width, 0, -self.titleLabel.bounds.size.width)];
}

@end
